package com.timoon.component.panel.nav;

import java.util.List;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;

import com.timoon.app.utils.Constantor;
import com.timoon.component.panel.nav.attachment.LiContainer;
import com.timoon.sample.dao.model.Resource;

/**
 *<p>横向导航面板基类</p>
 *<p></p>
 *<p>Copyright:Copyright(c)2013.4.1</p>
 *<p>Company:timoon</p>
 *@author 付乐天
 *@version 1.0 */
public abstract class AbstractHeaderNavPanel extends Panel {

	/**
	 * 
	 * @param id
	 * @param codes 所有链接的code，会自动根据session 里的ResourceMapper来判断链接的显隐
	 */
	public AbstractHeaderNavPanel(String id ,List<String> codes) {
		super(id);
		// TODO Auto-generated constructor stub
		
		final ResourceMapper mapper=(ResourceMapper) this.getSession().getAttribute(Constantor.SESSION_USER_MAPPER);
		
		ListView<String> list=new ListView<String>("wk_menu_list",codes) {

			@Override
			protected void populateItem(ListItem arg0) {
				// TODO Auto-generated method stub
				String code=(String) arg0.getDefaultModelObject();
				LiContainer li=new LiContainer("wk_menu_container", mapper.getResourceMap(), code);
				arg0.add(li);
				
				Resource resource=mapper.getResourceMap().get(code);
				String label=(resource==null?"":resource.getText());
				
				final Label span=new Label("wk_menu_text",label);
				span.setOutputMarkupId(true);
				
				AjaxLink link=getNavLink(code,span);
				if(link==null){
					link=new AjaxLink("wk_menu"){
						@Override
						public void onClick(AjaxRequestTarget arg0) {
							// TODO Auto-generated method stub
							span.add(new AttributeModifier("style","color:red;"));
							arg0.add(span);
						}
					};
				}
				li.add(link);
				
				link.add(span);
			}
		};
		add(list);
	}
	
	/**
	 * 根据code的不用来获取AbstrackNavLink
	 * @param code
	 * @param resource
	 * @return
	 */
	protected abstract AbstractNavLink getNavLink(String code,Label span);
	
	/**
	 * 每一次link点击是调用此方法
	 *使点击span高亮
	 *取消之前span高亮
	 * @param arg0
	 * @param span
	 */
	protected void changeSpanStyle(AjaxRequestTarget arg0,Label span){
		//取出之前高亮的span
		Label lastSpan=(Label) this.getSession().getAttribute("highlight-span-header");
		if(lastSpan!=null){
			lastSpan.add(new AttributeModifier("style","color:000000;"));
			arg0.add(lastSpan);
		}
		span.add(new AttributeModifier("style","color:red;"));
		arg0.add(span);
		this.getSession().setAttribute("highlight-span-header", span);

	}

}
